package com.zzz.email.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * 邮件配置类
 */
@Configuration
public class EmailConfig {
    
    @Value("${spring.mail.host:smtp.qq.com}")
    private String host;
    
    @Value("${spring.mail.port:465}")
    private Integer port;
    
    @Value("${spring.mail.username:}")
    private String username;
    
    @Value("${spring.mail.password:}")
    private String password;
    
    @Value("${spring.mail.protocol:smtp}")
    private String protocol;
    
    @Value("${spring.mail.default-encoding:UTF-8}")
    private String defaultEncoding;
    
    @Value("${email.from:${spring.mail.username}}")
    private String from;
    
    @Value("${email.send-message:true}")
    private boolean sendMessage;
    
    @Value("${email.verification-code.length:6}")
    private Integer verificationCodeLength;
    
    @Value("${email.verification-code.expire-minutes:5}")
    private Integer verificationCodeExpireMinutes;
    
    @Value("${email.verification-code.subject:OJ编程平台 - 邮箱验证码}")
    private String verificationCodeSubject;
    
    @Value("${email.verification-code.template-type:html}")
    private String verificationCodeTemplateType;
    
    @Value("${email.verification-code.template-path:classpath:templates/verification-code.html}")
    private String verificationCodeTemplatePath;
    
    @Value("${email.verification-code.template:您的验证码是：%s，请在%d分钟内使用。}")
    private String verificationCodeTemplate;
    
    @Value("${email.company-name:OJ编程平台}")
    private String companyName;
    
    /**
     * 创建邮件发送器
     */
    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        mailSender.setProtocol(protocol);
        mailSender.setDefaultEncoding(defaultEncoding);
        
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.port", String.valueOf(port));
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.starttls.required", "true");
        
        mailSender.setJavaMailProperties(properties);
        return mailSender;
    }
    
    // Getters
    public String getHost() {
        return host;
    }
    
    public Integer getPort() {
        return port;
    }
    
    public String getUsername() {
        return username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public String getProtocol() {
        return protocol;
    }
    
    public String getDefaultEncoding() {
        return defaultEncoding;
    }
    
    public String getFrom() {
        return from;
    }
    
    public boolean isSendMessage() {
        return sendMessage;
    }
    
    public Integer getVerificationCodeLength() {
        return verificationCodeLength;
    }
    
    public Integer getVerificationCodeExpireMinutes() {
        return verificationCodeExpireMinutes;
    }
    
    public String getVerificationCodeSubject() {
        return verificationCodeSubject;
    }
    
    public String getVerificationCodeTemplate() {
        return verificationCodeTemplate;
    }
    
    public String getVerificationCodeTemplateType() {
        return verificationCodeTemplateType;
    }
    
    public String getVerificationCodeTemplatePath() {
        return verificationCodeTemplatePath;
    }
    
    public String getCompanyName() {
        return companyName;
    }
}